package com.cmeduSystem.common.online.script;

import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.annotations.JSConstructor;
import org.mozilla.javascript.annotations.JSFunction;
import org.mozilla.javascript.annotations.JSGetter;

/**
 * MyPageData的JavaScript转换器
 *
 * @author xulei
 * @date 2025-09-04
 */
public class JsPageData extends ScriptableObject {

    private NativeArray dataList;
    private int totalCount;

    @Override
    public String getClassName() {
        return "MyPageData";
    }

    @JSConstructor
    public JsPageData() {
        this.totalCount = 0;
    }

    @JSConstructor
    public JsPageData(NativeArray dataList, int totalCount) {
        this.dataList = dataList;
        this.totalCount = totalCount;
    }

    @JSGetter
    public NativeArray getDataList() {
        return this.dataList;
    }

    @JSGetter
    public int getTotalCount() {
        return this.totalCount;
    }

    @JSFunction
    public void setDataList(NativeArray dataList) {
        this.dataList = dataList;
    }

    @JSFunction
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * 将MyPageData类转换为JavaScript构造函数
     */
    public static void convertToJsConstructor(Scriptable scope) {
        try {
            // 使用defineClass方法将MyPageData类暴露给JavaScript
            ScriptableObject.defineClass(scope, JsPageData.class);
        } catch (Exception e) {
            throw new RuntimeException("Failed to define MyPageData class in JavaScript: " + e.getMessage(), e);
        }
    }
}